<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="//at.alicdn.com/t/font_2409767_nfu0cir0odq.css">
    <style>
        * {
            padding: 0;
            margin: 0;
            user-select: none;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        body {
            overflow: hidden;
            background-color: #000;
        }

        .iconfont {
            font-size: 80px;
            color: red;
        }

        .virus {
            width: 80px;
            height: 80px;
            /* background-color: red; */
            position: absolute;
            left: 0;
            top: 0;
        }

        .virus p {
            position: absolute;
            width: 36px;
            height: 34px;
            background-color: #000;
            text-align: center;
            line-height: 36px;
            border-radius: 30px;
            font-size: 28px;
            font-weight: 800;
            left: 23px;
            top: 25px;
            color: red;
        }

        #stage {
            width: 375px;
            height: 100%;
            margin: 0 auto;
            box-shadow: 0 0 5px #aaa;
            position: relative;
        }

        #game {
            width: 100%;
            height: 100%;
            position: absolute;
        }

        #ui {
            width: 100%;
            height: 100%;
            position: absolute;
            z-index: 10;
        }

        #ui #score-label {
            position: absolute;
            z-index: 20;
            color: red;
            font-size: 50px;
            left: 20px;
            top: 20px;
        }

        #ui #start-alert {
            width: 100%;
            height: 100%;
            position: absolute;
            background-color: #fff;
            z-index: 30;
        }

        #ui #start-alert .game-desc {
            width: 80%;
            margin: 100px auto;
            text-align: center;
        }

        #ui #start-alert .game-desc h1 {
            background-color: #f00;
            width: 150px;
            margin: 0 auto;
            color: #fff;
        }

        #ui #start-alert .game-desc p {
            font-size: 18px;
            color: #333;
            font-weight: 300;
            line-height: 30px;
            margin-top: 20px;
        }

        #ui #start-alert #start-btn {
            margin: 0 auto;
            font-size: 30px;
            color: #444;
            font-weight: 300;
            text-align: center;
            text-decoration-line: underline;
        }

        /* 任务失败页面 */
        #ui #game-over-alert{
            width: 240px;
            height: 80px;
            border: solid 1px #fff;
            color: #fff;
            text-align: center;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left:-120px;
            margin-top: -100px;
            z-index: 30;
            display: none;
        }
        #ui #game-over-alert h1{
           
            background-color: #000;
            width: 150px;
            margin: 0 auto;
            margin-top: -30px;
        }

        #ui #game-over-alert #restart-btn{
            font-size: 24px;
            margin-top: 10px;
        }

        .warning {
            width: 100%;
            height: 100%;
            position: absolute;
            background-image: linear-gradient(to right, rgba(255, 0, 0, .5) 0%, rgba(255, 0, 0, 0) 20%, rgba(0, 0, 0, 0) 80%, rgba(255, 0, 0, .5) 100%);
            z-index: 10;
            animation: color-fade 0.5s 3;
        }

        @keyframes color-fade {
            0% {
                opacity: 0;
            }

            50% {
                opacity: 1;
            }

            100% {
                opacity: 0;
            }
        }
    </style>
</head>

<body>


    <div id="stage">

        <div id="game"></div>
        <div id="ui">
           <!-- 分数 -->
            <h1 id="score-label">0</h1>

            <!-- 开始页面 -->
            <div id="start-alert">
                <div class="game-desc">
                    <h1>游戏说明</h1>
                    <p>屏幕上方有带字母的“病毒”落下，按下键盘对应的字母，消灭病毒，否则，你将被“病毒”消灭😜</p>
                </div>
                <div id="start-btn">开始游戏</div>
            </div>

            <!-- 游戏失败页面 -->
            <div id="game-over-alert">
                <h1>任务失败</h1>
                <div id="restart-btn">重 玩</div>
            </div>
        </div>
        <!-- <div class="virus">
            <p>A</p>
            <span class="iconfont icon-bingdu"></span>
        </div> -->

        <!-- <div class="warning"></div> -->
    </div>

    <script>
        let config = {
            status: 0, // 0：游戏未开始  1：游戏进行中 2： 游戏结束
            speed:2, //病毒下落速度
            interval:1500 //病毒生成时间间隔
        }
        let stage = document.getElementById('stage')
        let game = document.getElementById('game')
        let ui = document.getElementById('ui')
        let virues = []
        let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
            'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        ]
        let winH = document.body.offsetHeight;
        let score = 0;
        let scoreLabel = document.getElementById('score-label')


        let warningLayer = document.createElement('div')
        warningLayer.setAttribute('class', 'warning')

        let timer, updater;

        // 开始按钮
        let startBtn = document.getElementById('start-btn')
        // 开始页面
        let startAlert = document.getElementById('start-alert')
        // 游戏失败页面
        let gameOverAlert = document.getElementById('game-over-alert')
        // 重新开始按钮
        let restartBtn = document.getElementById('restart-btn')


        startBtn.onclick = function () {
            startGame()
            config.status = 1;
            startAlert.style.display = 'none';

        }
        restartBtn.onclick = function(){
            gameOverAlert.style.display = 'none'
            resetGame()
        }
        function resetGame(){
            config.status = 1;
            score = 0;
            scoreLabel.innerHTML = score;
            game.innerHTML = ''
            virues = []
            ui.removeChild(warningLayer)
            startGame()
        }

        function startGame() {
            timer = setInterval(function () {
                makeVirus()
            }, config.interval)

            updater = setInterval(function () {
                update()
            }, 16)
        }

        function makeVirus() {

            let virus = document.createElement('div')
            virus.setAttribute('class', 'virus')
            let letter = letters[Math.floor(Math.random() * 26)]
            virus.innerHTML = `<p>${letter}</p><span class="iconfont icon-bingdu"></span>`
            virus.style.left = Math.random() * 315 + 'px'
            virus.letter = letter;
            game.appendChild(virus)
            virues.push(virus)
        }


        function gameOver() {
            clearInterval(timer)
            clearInterval(updater)
            config.status = 3;
            gameOverAlert.style.display = 'block'
        }

        function update() {

            for (let i = 0; i < virues.length; i++) {
                let virus = virues[i];
                virus.style.top = virus.offsetTop + config.speed + 'px';

                if (virus.offsetTop >= (winH - 200) && !ui.warning) {
                    ui.appendChild(warningLayer)
                    ui.warning = true
                } else if (virus.offsetTop >= winH) {
                    // game over
                    gameOver()
                }
            }
        }

        window.addEventListener('keyup', function (e) {
            let key = e.key;
            for (let i = 0; i < virues.length; i++) {
                let virus = virues[i];
                if (virus.letter.toLowerCase() == key.toLowerCase()) {
                    game.removeChild(virus)
                    virues.splice(i, 1)
                    score++;
                    scoreLabel.innerHTML = score;
                }
            }
        })
    </script>
</body>

</html>